package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.SetmealAndCheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealAndCheckGroup;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealAndCheckGroupMapper setmealAndCheckGroupMapper;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        IPage<Setmeal> p = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        if (queryPageBean.getQueryString() != null) {
            wrapper.eq(Setmeal::getCode, queryPageBean.getQueryString()).or()
                    .like(Setmeal::getName, queryPageBean.getQueryString()).or()
                    .eq(Setmeal::getHelpCode, queryPageBean.getQueryString());
        }
        IPage<Setmeal> setmealPage = setmealMapper.selectPage(p, wrapper);
        PageResult pageResult = new PageResult(setmealPage.getTotal(), setmealPage.getRecords());
        return pageResult;
    }

    @Override
    public Result add(Integer[] checkgroupIds, Setmeal setmeal) {
        //先添加套餐信息
        setmealMapper.insert(setmeal);
        //根据套餐id添加查询组
        for (Integer checkgroupId : checkgroupIds) {
            SetmealAndCheckGroup setmealAndCheckGroup = new SetmealAndCheckGroup(setmeal.getId(), checkgroupId);
            setmealAndCheckGroupMapper.insert(setmealAndCheckGroup);
        }
        return new Result(true, "添加成功");
    }

    @Override
    public Result findById(Integer id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        return new Result(true, "操作成功", setmeal);
    }

    @Override
    public Result update(Integer[] checkgroupIds, Setmeal setmeal) {
        //先更新套餐信息
        setmealMapper.updateById(setmeal);
        //检查组先删除原有的
        LambdaQueryWrapper<SetmealAndCheckGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealAndCheckGroup::getSetmealId, setmeal.getId());
        setmealAndCheckGroupMapper.delete(wrapper);
        //将前端返回的重新添加z
        for (Integer checkgroupId : checkgroupIds) {
            SetmealAndCheckGroup setmealAndCheckGroup = new SetmealAndCheckGroup(setmeal.getId(), checkgroupId);
            setmealAndCheckGroupMapper.insert(setmealAndCheckGroup);
        }
        return new Result(true, "修改成功");
    }

    @Override
    public void remove(Integer id) {
        //根据id删除检查项里的检查组
        LambdaQueryWrapper<SetmealAndCheckGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealAndCheckGroup::getSetmealId, id);
        setmealAndCheckGroupMapper.delete(wrapper);
        //删除检查项
        setmealMapper.deleteById(id);
    }
}
